package com.feather.finder;

import java.sql.*;

import javax.sql.DataSource;

import net.sf.json.JSONObject;

import com.feather.core.ActionFinder;

public class SQLActionFinder implements ActionFinder {

	private DataSource dataSource;
	
	private String sql;
	
	@Override
	public Object find(String uri) {
		Object action = null;
		try {
			PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);
			ps.setString(1, uri);
			ResultSet rs = ps.executeQuery();
			if(rs.next())
			{
				action = rs.getString(1);
				
				if(action != null)
				{
					Object newAction = JSONObject.fromObject(action);
					if(newAction != null)
						action = newAction;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return action;
	}

	public String getSql() {
		return sql;
	}

	public void setSql(String sql) {
		this.sql = sql;
	}

	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

}
